﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using WMSSoft.Lib.Basic;
using WMSSoft.Lib.Enums;

namespace WMSSoft.Lib.WinKStart
{
    public class StartmenuSearch
    {
        private cStartmenuItem m_SearchResult = null;

        public StartmenuSearch()
        {
            try
            {
                m_SearchResult = new cStartmenuItem();
            }
            catch (Exception Ex)
            {
                throw new Exception("Error while initializing Start-menu search!", Ex);
            }
        }

        public cStartmenuItem SearchResult
        {
            get { return m_SearchResult;  }
        }

        public void SearchItem(cStartmenuItem Menu, string Match)
        {
            try
            {
                if (Menu != null)
                {
                    Dictionary<string, object>.KeyCollection MenuChildItems = Menu.ChildItems.Keys ;
                    //process each child menuitem
                    foreach (string Item in MenuChildItems)
                    {
                        if (((cStartmenuItem)Menu.ChildItems[Item]).ItemType == eItemType.Link)
                        {
                            if (((cStartmenuItem)Menu.ChildItems[Item]).ViewName.ToUpper().IndexOf(Match.ToUpper()) > -1)
                            {
                                //item is matching with search add this item to search result
                                if (m_SearchResult.ChildItems.ContainsKey(((cStartmenuItem)Menu.ChildItems[Item]).ItemName) == false)
                                    m_SearchResult.AddChild(((cStartmenuItem)Menu.ChildItems[Item]));
                            }
                        }
                        else if (((cStartmenuItem)Menu.ChildItems[Item]).ItemType == eItemType.Folder || ((cStartmenuItem)Menu.ChildItems[Item]).ItemType == eItemType.None ||
                            ((cStartmenuItem)Menu.ChildItems[Item]).ItemType == eItemType.Filter)
                        {
                            SearchItem(((cStartmenuItem)Menu.ChildItems[Item]), Match);
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                WMSSoft.Lib.Basic.CMessagelogging.AppendMessage("Error while searching MenuItem!", Ex, Basic.EMessageTypes.Error);
            }
        }

    }
}
